<template>
    <div class="logincontainner">
       <div class="logincontent">
           <div class="loginhander">
                <img src="../assets/login.jpg"/>
           </div>
         <div class="loginForm">
             <!-- 登录表单 -->
           <el-form ref="LoginFormref" :model="loginForm" :rules="rules">
            <el-form-item  prop="username" >
             <el-input v-model="loginForm.username"  prefix-icon="el-icon-user" ></el-input>
            </el-form-item>
            <el-form-item prop="password">
             <el-input prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
            </el-form-item>
           </el-form>
          
           <div  class="loginButton">
            <el-button type="primary" @click="loginRight">确认</el-button>
           <el-button type="danger" @click="reset">重置</el-button>
           </div>
           
         </div>
          
       </div>
       
    </div>
</template>
<script>
export default {
   name: 'login',
   data(){
       return{
           loginForm: {
               username: '',
               password: ''
           },
           rules: {
             username: [
                 { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
                { required: true, message: '请输入用户名', trigger: 'blur' }
                
          ],
          password: [
                { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
                { required: true, message: '请输入密码', trigger: 'blur' }
          ]

       },
      value: true
   }

},
methods: {
    reset(){
       this.$refs.LoginFormref.resetFields()
    },
    loginRight(){
        this.$refs.LoginFormref.validate(async validate => {
             // eslint-disable-next-line no-useless-return
             if(!validate) return 
             if(validate) { 
                const  { data }  = await this.$http.post('http://q8.shaohu.host:8888/login' , this.loginForm)
                   if(data.code === 200){
                     const token = data.data
                     window.sessionStorage.setItem('token',token)
                     window.sessionStorage.setItem('name',this.loginForm.username)
                     this.$msg.success('登录成功')
                     this.$router.push('/home')
                   }else{
                     this.$msg.error('登录失败')
                   }
             }
        })
    }
}
}
</script>
<style lang="less" scoped>
.logincontainner{
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    position: relative;
    .logincontent{
        width: 350px;
        height: 400px;
        background-color: white;
        position: absolute;
        top:0px;
        left: 0px;
        bottom: 0px;
        right: 0px;
        margin: auto;
        .loginhander{
            position: absolute;
            left:50%;
            border-radius: 50%;
            border: 5px solid #fff;
            box-shadow: 0px 0px 10px #888888;
            transform: translate(-50%,-50%);
            width: 130px;
            height: 130px;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
                
            }
        }
        .loginForm{
            position: absolute;
            top:50%;
            transform: translateY(-50%);
            padding: 0px 20px;
            width: 100%;
            box-sizing: border-box;
            .showPassword{
                position: absolute;
                top:50%;
                width: 50px;
                height: 50px;
                background-color: aqua;

            }
            .loginButton{
                display: flex;
                justify-content: space-between;
                :nth-child(1){
                    width: 40%;

                }
                :nth-child(2){
                    width: 40%;
                    
                }
            }
        }

    }
}
</style>
